package com.ebelter.btcomlib.utils;

import android.support.v7.widget.RecyclerView;
import android.text.InputFilter;
import android.text.Spanned;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ebelter.btcomlib.R;

import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class ViewUtils {


    //设置views是否选中状态
    public static void setSelects(boolean seselect,View... views){
        for (View v:views){
            setSelect(v,seselect);
        }
    }

    //设置view是否选中状态
    public static void setSelect(View view,boolean select){
        if (view!=null) view.setSelected(select);
    }

    public static void setBackgroundColor(View view,int color){
        if (view!=null){
        view.setBackgroundColor(color);
        }
    }


    //RecyclerView 滚动到指定位置 不带滚动效果
    public static void recyclerViewScrollToPosition(RecyclerView recyclerView, int position) {
        if (recyclerView==null){
            return;
        }
        int count=recyclerView.getAdapter().getItemCount();
        if (count==0){
            return;
        }
        if (position>=count){
            position=count-1;
        }else if (position<0){
            position=0;
        }
        recyclerView.scrollToPosition(position);
    }

    /**
     * 让ViewGround和它的子View全部选中或者不选中
     */
    public static void childSelected(ViewGroup viewGroup, boolean selected) {
        if (viewGroup != null) {
            viewGroup.setSelected(selected);
            for (int i = 0; i < viewGroup.getChildCount(); i++) {
                View childView = viewGroup.getChildAt(i);
                if (childView != null) childView.setSelected(selected);
            }
        }

    }



    //使RadioGroup不可点击
    public static void setRadioGroupEnable(RadioGroup testRadioGroup,boolean enable) {
        if (testRadioGroup==null){
            return;
        }
        for (int i = 0; i < testRadioGroup.getChildCount(); i++) {
            testRadioGroup.getChildAt(i).setEnabled(enable);
        }
    }

    private static Animation shake;
    //使控件抖动
    public static void shakeView(View view){
        if (view==null)return;
        if (shake==null){
            shake = AnimationUtils.loadAnimation(view.getContext().getApplicationContext(), R.anim.shake);//加载动画资源文件
        }
        view.startAnimation(shake);

    }

    /**
     * 隐藏控件
     */
    public static void hideView(View view) {
        if (view != null && view.getVisibility() != View.INVISIBLE) {
            view.setVisibility(View.INVISIBLE);
        }
    }

    /**
     * gone控件
     */
    public static void goneView(View view) {
        if (view != null && view.getVisibility() != View.GONE) {
            view.setVisibility(View.GONE);
        }
    }

    //设置控件透明度
    public static void setAlpha(View view,float alpha) {
        if (view != null) {
            view.setAlpha(alpha);
        }
    }

    /**
     * 显示控件
     */
    public static void displayView(View view) {
        if (view != null && view.getVisibility() != View.VISIBLE) {
            view.setVisibility(View.VISIBLE);
        }
    }

    public static void setEnabled(View view,boolean enabled) {
        if (view != null ) {
            view.setEnabled(enabled);
        }
    }

    /**设置权重*/
    public static void setWeight(View view ,float weight){
        if (view==null)return;
        ViewGroup.LayoutParams lp = view.getLayoutParams();
        if (lp!=null&&lp instanceof LinearLayout.LayoutParams){
            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) view.getLayoutParams();
            layoutParams.weight=weight;
            view.setLayoutParams(layoutParams);
        }

    }





    //获取测量的高度
    public static int getMeasuredHeight(View view){
        if (view==null){
            return 0;
        }
        return view.getMeasuredHeight();
    }

    //获取测量的宽度
    public static int getMeasuredWidth(View view){
        if (view==null){
            return 0;
        }
        return view.getMeasuredWidth();
    }



    //设置控件的宽高度
    public static void setViewWidthHeight(View view,int width,int height){
        ViewGroup.LayoutParams params = view.getLayoutParams();
        params.width = width;
        params.height = height;
        view.setLayoutParams(params);
    }
    public static void setViewHeight(View view,int height){
        if (view==null){
            return;
        }
        ViewGroup.LayoutParams params = view.getLayoutParams();
        if (params==null){
            params=new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        }
        params.height = height;
        view.setLayoutParams(params);
    }


    public static void setViewWidthHeightSame(View view,int width){
        setViewWidthHeight(view,width,width);
    }


    //listView根据itemView的高度来自适应 好像不太适合用有头尾的listView
    public static void setListViewHeightBasedOnChildren(ListView listView) {
        //获取ListView对应的Adapter
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            // pre-condition
            return;
        }

        int totalHeight = 0;
        for (int i = 0, len = listAdapter.getCount(); i < len; i++) {   //listAdapter.getCount()返回数据项的数目
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);  //计算子项View 的宽高
            totalHeight += listItem.getMeasuredHeight();  //统计所有子项的总高度
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        //listView.getDividerHeight()获取子项间分隔符占用的高度
        //params.height最后得到整个ListView完整显示需要的高度
        listView.setLayoutParams(params);
    }


    //设置title1
    public static void setTextViewStr(TextView tv, String str){
        if (tv!=null)tv.setText(str);
    }
    //设置title1
    public static void setTextViewStr(TextView tv, int res){
        if (tv!=null)tv.setText(res);
    }

    public static void setTextColor(TextView tv, int color){
        if (tv!=null)tv.setTextColor(color);
    }

    public static void setTextSize(TextView tv,int sp){
        if (tv==null)return;
        tv.setTextSize(TypedValue.COMPLEX_UNIT_SP,sp);
    }


    public static void setImageResource(ImageView imageView,int res){
        if (imageView==null)return;
        imageView.setImageResource(res);
    }

    public static void setBgResource(View view,int res){
        if (view==null)return;
        view.setBackgroundResource(res);
    }



    public static void setBottomMargin(View view,int dp){
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
        layoutParams.bottomMargin=DipUtil.dip2px(view.getContext().getApplicationContext(),dp);//将默认的距离底部20dp，改为0，这样底部区域全被listview填满。
        view.setLayoutParams(layoutParams);
    }



    public static final InputFilter[] inputFilters =new InputFilter[]{new InputFilter() {
       // final Pattern pattern = Pattern.compile("[^a-zA-Z0-9\\u4E00-\\u9FA5_]");
        final Pattern pattern = Pattern.compile(regex);
        @Override
        public CharSequence filter(CharSequence charSequence, int i, int i1, Spanned spanned, int i2, int i3) {
            Matcher matcher=  pattern.matcher(charSequence);
            if(!matcher.find()){
                return null;
            }else{
                return "";
            }
        }
    },new InputFilter.LengthFilter(20)};

    public static void setEdInputFilters(EditText ed){
        if (ed!=null)ed.setFilters(inputFilters);
    }


    public static final String regex="[^a-zA-Z0-9" +
            "\\u4E00-\\u9FA5" +//中文
            "\\u3040-\\u30ff" +//日文1
            "\\u31f0-\\u31ff" +//日文2
            "\\u1100-\\u11ff" +//韩文1
            "\\u3130-\\u318f" +//韩文2
            "\\uac00-\\ud7af" +//韩文3
            "\\u0e00-\\u0e7e" +//泰文1
            "\\u0e80-\\u0eff" +//寮文1
            "\\u0f00-\\u0fff" +//藏文1
            "\\ua000-\\ua4cf" +//彝文1
            "\\u1800-\\u18af" +//蒙古文1
            "\\u1000-\\u109f" +//缅甸文1
            "\\u1780-\\u17ff" +//高棉文1
            "\\u00c0-\\u02af" +//拉丁文1
            "\\u1e00-\\u1eff" +//拉丁文2
            "\\u0370-\\u03ff" +//希腊文1
            "\\u1f00-\\u1fff" +//希腊文2
            "\\u2c80-\\u2cff" +//希腊文3
            "\\u0590-\\u05ff" +//希伯来语1
            "\\u0600-\\u06ff" +//阿拉伯文1
            "\\u0750-\\u077f" +//阿拉伯文2
            "\\u0700-\\u074f" +//叙利亚1
            "\\u0400-\\u052f" +//西里爾文1
            "\\u0530-\\u058f" +//亚美尼亚文1
            "\\u0980-\\u09ff" +//孟加拉文文1
            "\\u0d80-\\u0dff" +//斯里兰卡文1
            "_]";
    public static void setEdInputFilters2(EditText ed){
        if (ed!=null)ed.setFilters(new InputFilter[]{new InputFilter() {
            final Pattern pattern2 = Pattern.compile("[^a-zA-Z0-9]");
            @Override
            public CharSequence filter(CharSequence charSequence, int i, int i1, Spanned spanned, int i2, int i3) {
                Matcher matcher=  pattern2.matcher(charSequence);
                if(!matcher.find()){
                    return null;
                }else{
                    return "";
                }
            }
        },new InputFilter.LengthFilter(20)});
    }
}
